﻿using CNav.Business.Services;
using CNav.Common;
using CNav.Controllers;
using CNav.Domain.Admin;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CNav.Support;
using WebGrease.Css.Extensions;

namespace CNav.Controllers
{
     [Authorize]
     [AdminAuthorize]
    public class CustomFormController : BaseController
    {
        public ActionResult Index()
        {
            var response = Service.Get<IUiService>().GetFormList();
            return View(response.Data);
        }

        public ActionResult Details(int id = 0)
        {
            var response = Service.Get<IUiService>().GetFormById(id);
            if (response.Data == null)
            {
                return View("NotFound");
            }
            return View(response.Data);
        }


        public ActionResult Create()
        {
            return View();
        }
         
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(Form form)
        {
            if (ModelState.IsValid)
            {
                Service.Get<IUiService>().AddForm(form);
                return RedirectToAction("Index");
            }

            return View(form);
        }

        public ActionResult Edit(int id = 0)
        {
            var response = Service.Get<IUiService>().GetFormById(id);
            if (response.Data == null)
            {
                return View("NotFound");
            }
            return View(response.Data);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(Form form)
        {
            if (ModelState.IsValid)
            {
                var response = Service.Get<IUiService>().UpdateForm(form);
                if (response.Error == null)
                    return RedirectToAction("Index");

                response.Error.ForEach(e => ModelState.AddModelError("", e));
            }

            return View(form);
        }


        public ActionResult Delete(int id = 0)
        {
            var response = Service.Get<IUiService>().GetFormById(id);

            if (response.Data == null)
            {
                return View("NotFound");
            }
            return View(response.Data);
        }

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            ResponseData<bool> responseData = Service.Get<IUiService>().DeleteForm(id);
            if(responseData.Data)
                return RedirectToAction("Index");

            responseData.Error.ForEach(e => ModelState.AddModelError("", e));
            var response = Service.Get<IUiService>().GetFormById(id);

            if (response.Data == null)
            {
                return View("NotFound");
            }
            return View(response.Data);
        }
    }
}